package main;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.junit.Test;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;

public class Main {

	public static URL getClassPathResource(String path) {
		return Main.class.getClassLoader().getResource(path);
	}

	public static void main(String[] args) throws Exception {
		Reader reader = new QRCodeReader();
		BufferedImage myImage = ImageIO
				.read(getClassPathResource("resources/simple.png"));
		LuminanceSource source = new BufferedImageLuminanceSource(myImage);
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
		Result result = reader.decode(bitmap);
		String text = new String(result.getText().getBytes(), "UTF-8");
		System.out.println(text);
		
		QRCodeWriter writer = new QRCodeWriter();
		Hashtable hints = new Hashtable();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hints);
		BufferedImage mimg = MatrixToImageWriter.toBufferedImage(bitMatrix);
		ImageIO.write(mimg, "jpg", new FileOutputStream("c://simple3.jpg"));
		
	}
	
	@Test
	public void testWirte() throws WriterException, Exception {
		QRCodeWriter writer = new QRCodeWriter();
		Hashtable hints = new Hashtable();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix = writer.encode("趁着光彩d", BarcodeFormat.QR_CODE, 300, 300, hints);
		BufferedImage myImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
		ImageIO.write(myImage, "jpg", new FileOutputStream("c://simple2.jpg"));
	}

}
